 <template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="关联类型">
          <el-select size="small" v-model="query.fkType" placeholder="请选择" clearable>
            <el-option v-for="dict in dict.type.attachment_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资料名称">
          <el-input size="small" v-model="query.name" placeholder="资料名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-operate">
        <el-button @click="showAddView" type="primary" size="small" icon="el-icon-plus">
          新增
        </el-button>
      </div>
      <el-table header-row-class-name="container-table-header" :data="data" size="small" style="width: 100%;margin-bottom: 20px;"
                row-key="id" border stripe v-loading="loading">
        <el-table-column prop="fkType" label="关联类型">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.attachment_type" :value="scope.row.fkType"/>
          </template>
        </el-table-column>
        <el-table-column prop="attachmentType" label="资料类型">
        </el-table-column>
        <el-table-column prop="name" label="资料名称">
        </el-table-column>
        <el-table-column prop="accept" label="文件格式">
        </el-table-column>
        <el-table-column prop="required" label="是否必传">
          <template slot-scope="scope">
            <el-tag :type="scope.row.required | tureOrFalseTag">{{ scope.row.required | tureOrFalse }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170">
          <template slot-scope="scope">
            <el-button v-if="permissions.attachment_template_add" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑
            </el-button>
            <el-button v-if="permissions.attachment_template_del" type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container-page">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="formTitle" :visible.sync="formBox" :close-on-click-modal="!formBox" :append-to-body="true" :before-close="resetForm" width="800px" top="5vh">
      <el-form ref="optForm" :model="form" label-width="90px">
        <el-form-item label="关联类型" prop="fkType" :rules="{required: true, message: '关联类型不能为空', trigger: ['blur']}">
          <el-select v-model="form.fkType">
            <el-option v-for="dict in dict.type.attachment_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资料类型" prop="attachmentType">
          <el-input placeholder="资料类型" clearable v-model="form.attachmentType"/>
        </el-form-item>
        <el-form-item label="资料名称" prop="name" :rules="{required: true, message: '资料名称不能为空', trigger: ['blur']}">
          <el-input placeholder="资料名称" clearable v-model="form.name"/>
        </el-form-item>
        <el-form-item label="文件格式" prop="accept">
          <el-input placeholder="文件格式" clearable v-model="form.accept"/>
        </el-form-item>
        <el-form-item label="是否必传" prop="required">
          <el-radio-group v-model="form.required">
            <el-radio-button :label="0">否</el-radio-button>
            <el-radio-button :label="1">是</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
        <el-button v-if="!isView" type="primary" @click="submit" size="small" icon="el-icon-check">保存</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import Tinymce from '@/components/Tinymce'
import {delObj, fetchList, getObj, submit} from "@/api/adis/base/attachmentTemplate";

export default {
  directives: {
  },
  components: {
    Tinymce,
  },
  dictTypes: ['attachment_type'],
  data() {
    return {
      importBox: false,
      isEdit: false,
      isView: false,
      viewBox: false,
      formBox: false,
      formTitle: '',
      loading: false,
      query: {},
      form: {
      },
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      selectionRow: [],
      Authorization: '',
      fileList: [], // 文件
    }
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods: {
    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.fileList && this.fileList.map((file) => {
            this.form.fileUrl = file.response ? file.response.data.url : file.url
          });
          this.loading = true;
          submit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    showAddView() {
      this.formTitle = '新增';
      this.formBox = true;
      this.form = {};
    },
    showEditBox(row) {
      getObj(row.templateId).then((res) => {
        this.form = res.data.data;
        if(res.data.data.fileUrl) {
          this.fileList.push({url:res.data.data.fileUrl, name: res.data.data.title});
        }
        this.formTitle = '编辑';
        this.formBox = true;
        this.isEdit = true;
      });
    },
    showViewBox(row) {
      getObj(row.templateId).then((res) => {
        this.form = res.data.data;
        this.viewBox = true;
        this.isView = true;
      });
    },
    handleDelete(row) {
      this.$confirm("确定将选择的数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delObj(row.templateId);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    resetForm() {
      this.formBox = false;
      this.viewBox = false;
      this.isEdit = false;
      this.isView = false;
      this.form = {};
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.printBox = false;
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if(this.query.publishDates) {
        this.query.startDate = this.query.publishDates[0];
        this.query.endDate = this.query.publishDates[1];
      }
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    handleSuccess(response, file, fileList) {
      console.log(response, file, fileList)
      this.fileList = fileList;
      this.form.filePath = response.filePath;
    },
    handlePreview() {
      this.$message({
        type: "warning",
        message: "你最多只能上传1个附件"
      });
      // window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      this.$confirm('确认删除附件?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.fileList = [];
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    webOffice(url) {
      this.$router.push({path:'/webOffice'})
    }
  }
};
</script>

<style>
</style>
